package com.xiaochong.tir.manager.controller;

import com.github.pagehelper.PageInfo;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.po.ActivityNotice;
import com.xiaochong.tir.common.data.po.SyResearchReport;
import com.xiaochong.tir.common.data.vo.ActivityNoticeVo;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import com.xiaochong.tir.manager.service.ActivityNoticeService;
import com.xiaochong.tir.manager.service.SyResearchReportService;
import com.xiaochong.tir.manager.util.ManagerVersion;
import com.xiaochong.tir.manager.util.ReturnResult;
import com.xiaochong.tir.manager.util.SessionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: WangXing
 * @Description:
 * @CreateDate: 2018/8/4 10:22
 */
@RestController
@RequestMapping(ManagerVersion.API_V1_PREFIX+"/notice/")
@Api(value = "活动通知")
public class ActivityNoticeController {

    @Autowired
    private ActivityNoticeService activityNoticeService;

    @Autowired
    private SyResearchReportService syResearchReportService;

    @Autowired
    private RedisServer redisServer;
    /**
     * 活动通知列表页面
     * @return
     */
    @GetMapping("anotice")
    public Object aNotice(){
        return new ModelAndView("atnotice/anoticepage");
    }

    /**
     * 查询活动通知列表
     * @param vo
     * @return
     */
    @ApiOperation(value = "查询活动通知列表",notes = "查询活动通知列表")
    @PostMapping("selaNotice")
    public Object activityNoticeList(ActivityNoticeVo vo){
        PageInfo<ActivityNotice> info = activityNoticeService.selectByAnoticeList(vo);
        return new ReturnResult<ActivityNotice>(0,"成功",info.getList(),info.getTotal());
    }

    /**
     * 发布活动通知页面
     * @return
     */
    @GetMapping("addaNotice")
    public Object addaNotice(){
        return new ModelAndView("atnotice/addnoticepage");
    }

    /**
     * 发布活动通知
     * @param activityNotice
     * @return
     */
    @ApiOperation(value = "发布活动通知",notes = "发布活动通知")
    @PostMapping("insaNotice")
    public Object insaNotice(ActivityNotice activityNotice){
        redisServer.set(RedisKeysUtils.LOGINNAME,"发布活动通知");
        ActivityNotice old = activityNoticeService.selectByEntity(activityNotice);
        if(null!=old){
            return new ReturnResult<ActivityNotice>(-1,"重复添加",1,1);
        }
        activityNotice.setCreateTime(new Date());
        if(activityNotice.getPublishMode().equals("1")){
            activityNotice.setPublishTime(new Date());
        }
        activityNoticeService.insertSelective(activityNotice);
        return new ReturnResult<ActivityNotice>(0,"成功",1,1);
    }

    /**
     * 查询所有研报的标题和id列表
     * @return
     */
    @ApiOperation(value = "查询所有研报的标题和id列表",notes = "查询所有研报的标题和id列表")
    @PostMapping("selreportlist")
    public Object selreportlist(){
        List<SyResearchReport> list = syResearchReportService.selreportlist(new SyResearchReport());
        List<SyResearchReport> res = new ArrayList<>();
        if(null !=list){
            for (int i = 0; i <list.size() ; i++) {
                SyResearchReport report = list.get(i);
                if(StringUtils.isNotBlank(report.getAppTitle())){
                    res.add(report);
                }
            }
        }
        return res;
    }

    /**
     * 编辑页面
     * @param id
     * @return
     */
    @GetMapping("editanotice")
    public Object editActivityNotice(@RequestParam("id")Integer id){
        ActivityNotice notice = activityNoticeService.selectByPrimaryKey(id);
        ModelAndView mv = new ModelAndView("atnotice/editanoticepage");
        mv.addObject("notice",notice);
        return mv;
    }

    /**
     * 查看
     * @param id
     * @return
     */
    @ApiOperation(value = "查看活动通知",notes = "查看活动通知")
    @GetMapping("detilanotice")
    public Object detilActivityNotice(@RequestParam("id")Integer id){
        ActivityNotice notice = activityNoticeService.selectByPrimaryKey(id);
        if(StringUtils.isNotBlank(notice.getRelation())){
            if(notice.getRelation().equals("2")){
                SyResearchReport report =
                        syResearchReportService.selectByPrimaryKey(Integer.valueOf(notice.getRedirectUrl()));
                if(StringUtils.isNotBlank(report.getAppTitle())){
                    notice.setRedirectUrl(report.getAppTitle());
                }else{
                    notice.setRedirectUrl(null);
                }
            }
        }
        ModelAndView mv = new ModelAndView("atnotice/viewanoticepage");
        mv.addObject("notice",notice);
        return mv;
    }

    /**
     *编辑方法
     * @param activityNotice
     * @return
     */
    @ApiOperation(value = "编辑活动通知",notes = "编辑活动通知")
    @PostMapping("updateanotice")
    public Object updateActivityNotice(ActivityNotice activityNotice){
        redisServer.set(RedisKeysUtils.LOGINNAME,"编辑活动通知");
        ActivityNotice old = activityNoticeService.selectByPrimaryKey(activityNotice.getId());
        if(activityNotice.getPublishMode().equals("1")){
            activityNotice.setPublishTime(new Date());
        }
        activityNoticeService.updateByActivityNoticeKey(activityNotice,old);
        return new ReturnResult<ActivityNotice>(0,"成功",1,1);
    }
}
